﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZK.HardWareHelp
{
    /// <summary>
    /// 地磅帮助类-非静态类
    /// </summary>
    public class WagonBalance
    {
        public static int dataNum = 0;//返回的第三位数据
        public SerialPort serialPort = null;//实例

        public WagonBalance(string portName)
        {
            serialPort = new SerialPort(portName);
        }
        public WagonBalance(string portName, int baudRate)
        {
            serialPort = new SerialPort(portName, baudRate);
        }
        public WagonBalance(string portName, int baudRate, Parity parity)
        {
            serialPort = new SerialPort(portName, baudRate, parity);
        }
        public WagonBalance(string portName, int baudRate, Parity parity, int dataBits)
        {
            serialPort = new SerialPort(portName, baudRate, parity, dataBits);
        }
        public WagonBalance(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
        {
            serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
        }
        public WagonBalance(Common.HaredWareCommonM config)
        {
            serialPort = new SerialPort(config.PortName, Convert.ToInt32(config.BaudRate), Parity.None, Convert.ToInt32(config.DataBits), StopBits.One);
        }
        /// <summary>
        /// 打开串口通讯
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            bool boolRes = true;
            try
            {
                if (!serialPort.IsOpen)
                {
                    serialPort.Open();
                }
                serialPort.DataReceived += SerialPort_DataReceived;
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("WagonBalance Open " + ex.ToString());
                boolRes = false;
            }
            return boolRes;
        }
        /// <summary>
        /// 读取数据委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //byte[] buffer = new byte[serialPort.BytesToRead];
            byte[] ReDatas = new byte[serialPort.BytesToRead];
            serialPort.Read(ReDatas, 0, ReDatas.Length);
            if (ReDatas.Length == 12)
            {
                var number = 0;
                number += (ReDatas[3] - 48) * 10000;
                number += (ReDatas[4] - 48) * 1000;
                number += (ReDatas[5] - 48) * 100;
                number += (ReDatas[6] - 48) * 10;
                number += ReDatas[7] - 48;
                //return number / 1000m;
                Common.DelegateHelper.WeightCallbackEvent?.Invoke(number);

            }
            //else
            //{
            //    LogManage.Err("获取地磅数据长度有误");
            //    //return 0;
            //    WeightCallback?.Invoke(0);
            //}
        }
        /// <summary>
        /// 关闭串口连接
        /// </summary>
        /// <returns></returns>
        public bool Close()
        {
            bool res = true;
            try
            {
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                }
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("WagonBalance Close " + ex.ToString());
                res = false;
            }
            return res;
        }
    }
}
